<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-01-04 15:43:50
 * @LastEditors: sunkaiyuan sunkaiyuan@namenode.com
 * @Description: 
 */

namespace App\Http\Services\System;

use App\Http\Services\Service;

use App\Repositories\BarcodeRepositorie;
use Illuminate\Support\Facades\Storage;

class BarcodeService extends Service
    {

    public $table;
    public function __construct(BarcodeRepositorie $repository)
        {
        $this->repository = $repository;
        }

    public function list()
        {
        $data = $this->repository->condition()->getGrid();
        return $data;
        }
    public function info()
        {
        return $this->repository->condition()->first();
        }

    protected function delete()
        {
        return $this->repository
            ->whereIn("id", $this->getRequestIds())
            ->get()
            ->filter(function ($v)
                {
                Storage::disk("barcode")->delete([$v->filepath]);
                $v->delete();
                });
        }
    protected function save()
        {
        $request = request();
        $name    = date("YmdHis") . rand(100, 999) . ".grf";
        $data    = $request->toArray();
        $path    = "{$request->code}/{$name}";

        if (isEmpty($request->id)) {
            $str = Storage::disk("barcode")->get("base.grf");

            Storage::disk("barcode")->put(
                "{$path}",
                $str
            );
            $data['filepath'] = $path;
            $data['filename'] = $name;
            }
        return $this->repository->autosave($data);
        }
    public function saveReport()
        {
        $request = request();

        $id = $request->input("id");

        $data = $this->repository->whereId($id)->first();

        if ($data == null)
            fail("未找到报表");
        // $path = Storage::saveFile("{$data->gridtable->code}", $data->filename);
        Storage::disk("barcode")->put("{$data->filepath}", $request->getContent());
        return $data->url;
        }
    }